using System;
using System.Collections.Generic;
using NHibernate.Mapping.Attributes;
using NHMA = NHibernate.Mapping.Attributes;

namespace SuperMarketManagement.Entity
{

    [Class(Name = "Category")]
    public class Category
    {
        [Id(1, Name = "AutoId"), Generator(2, Class = "native")]
        public virtual long AutoId { get; set; }
        [Property]
        public virtual string Name { get; set; }

        [NHMA.List(1, Cascade = "all-delete-orphan")]
        [NHMA.Key(2, Column = "ParentAutoId")]
        [NHMA.Index(3, Column = "ChildIndex")]
        [NHMA.OneToMany(4, ClassType = typeof(Category))]
        public virtual IList<Category> Children { get; set; }

        [ManyToOne(Column = "ParentAutoId")]
        public virtual Category Parent { get; set; }

        public Category()
        {
            Children = new List<Category>();
        }

        public Category(String name, params Category[] children)
            : this()
        {
            Name = name;
            foreach (Category child in children)
            {
                AddChild(child);
            }
        }

        public virtual void AddChild(Category cat)
        {
            Children.Add(cat);
            cat.Parent = this;
        }

        public virtual void RemoveChild(Category cat)
        {
            cat.Parent = null;
            Children.Remove(cat);
        }
    }
}
